package com.ulife.item.manager.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ulife.common.WebConstant;
import com.ulife.item.dao.ItemDao;
import com.ulife.item.dao.dataobject.ICSDo;
import com.ulife.item.dao.dataobject.IPVDo;
import com.ulife.item.manager.ItemManager;
import com.ulife.item.module.vo.ItemVo;

public class ItemManagerImpl implements ItemManager {

	private static final Logger log = LoggerFactory.getLogger(WebConstant.class);

	private ItemDao itemDao;

	private SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

	public boolean addItem(ItemVo itemVo) throws Exception {
		String f1path = writeImageFile(itemVo.getItemPicsFile1());
		String f2path = writeImageFile(itemVo.getItemPicsFile2());
		String f3path = writeImageFile(itemVo.getItemPicsFile3());

		String[] pics = {};
		if (StringUtils.isNotBlank(f1path)) {
			pics = (String[]) ArrayUtils.add(pics, f1path.substring(f1path.indexOf(WebConstant.ITEM_PIC_FOLDER), f1path.length()));
		}
		if (StringUtils.isNotBlank(f2path)) {
			pics = (String[]) ArrayUtils.add(pics, f2path.substring(f2path.indexOf(WebConstant.ITEM_PIC_FOLDER), f2path.length()));
		}
		if (StringUtils.isNotBlank(f3path)) {
			pics = (String[]) ArrayUtils.add(pics, f3path.substring(f3path.indexOf(WebConstant.ITEM_PIC_FOLDER), f3path.length()));
		}
		itemVo.setItemPics(StringUtils.join(pics, WebConstant.ITEM_PIC_SEPARATOR));
		itemDao.insertItem(itemVo);

		String pvv = itemVo.getItemPVvalues();
		if(StringUtils.isNotBlank(pvv)){
			String[] pvvarry = StringUtils.split(pvv, ";");
			for (int i = 0; i < pvvarry.length; i++) {
				IPVDo ipvDo = new IPVDo();
				ipvDo.setItemId(itemVo.getItemId());
				ipvDo.setPvId(Long.valueOf(pvvarry[i]));
				itemDao.insertIPV(ipvDo);
			}
		}
		
		Long catId = Long.valueOf(itemVo.getItemCatId());
		ICSDo icsDo = new ICSDo();
		icsDo.setcId(catId);
		icsDo.setItemId(itemVo.getItemId());
		icsDo.setsId(itemVo.getsId());
		itemDao.insertICS(icsDo);
		
		
		
		return true;
	}

	private String writeImageFile(FileItem fi1) throws Exception {
		if (fi1 != null) {
			Calendar now = Calendar.getInstance();
			File folder = new File(WebConstant.FILE_UPLOAD_PATH + WebConstant.ITEM_PIC_FOLDER + File.separator + now.get(Calendar.YEAR)
					+ (now.get(Calendar.MONTH) + 1));
			if (!folder.exists()) {
				log.warn("mkdir " + folder.getAbsolutePath() + ":" + folder.mkdirs());
			}
			String imgsuffix = fi1.getName().substring(fi1.getName().lastIndexOf("."), fi1.getName().length());
			File img = new File(folder.getAbsoluteFile() + File.separator + format.format(now.getTime()) + "-" + RandomStringUtils.randomNumeric(6)
					+ imgsuffix);
			fi1.write(img);
			return img.getAbsolutePath();
		} else {
			return null;
		}
	}
	
	public boolean deleteItem(ItemVo itemVo) throws Exception {
		if(itemDao.deleteItem(itemVo.getItemId())>0){
			return true;
		}else{
			return false;
		}
	}

	public void setItemDao(ItemDao itemDao) {
		this.itemDao = itemDao;
	}

}
